package com.itcat.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcat.api.mysql.UmsAdminApi;
import com.itcat.mapper.UmsAdminMapper;
import com.itcat.mapper.UmsAdminRoleMapper;
import com.itcat.mysql.pojo.UmsAdmin;
import com.itcat.util.AsyncTimeApi;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * [一句话描述该类的功能]
 *
 * @author : [Cr Mr]
 * @version : [v1.0]
 * @date : [2022/3/31 17:14]
 */
@DubboService
public class UmsAdminApiImpl implements UmsAdminApi {

    @Autowired
    private UmsAdminMapper umsAdminMapper;

    @Autowired
    private UmsAdminRoleMapper umsAdminRoleMapper;

    @Autowired
    private AsyncTimeApi asyncTimeApi;

    @Override
    public UmsAdmin selectByName(String username) {
        return umsAdminMapper.selectOne(new LambdaQueryWrapper<UmsAdmin>().
                eq(!StringUtils.isEmpty(username), UmsAdmin::getUsername, username)
        );
    }

    @Override
    public UmsAdmin selectLogin(String username) {
        return umsAdminMapper.selectOne(new LambdaQueryWrapper<UmsAdmin>().
                eq(!StringUtils.isEmpty(username), UmsAdmin::getUsername, username)
                .eq(UmsAdmin::getStatus, 1)
        );
    }

    @Override
    public Page<UmsAdmin> selectPage(String username, Integer pageNum, Integer pageSize) {
        Page<UmsAdmin> adminPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<UmsAdmin> lqw = new LambdaQueryWrapper<>();
        lqw.like(username != null && !username.equals(""), UmsAdmin::getUsername, username);
        Page<UmsAdmin> selectPage = umsAdminMapper.selectPage(adminPage, lqw);
        return selectPage;
    }

    @Override
    public void delete(Long id) {
        umsAdminMapper.deleteById(id);
        umsAdminMapper.deleteReoles(id);
        asyncTimeApi.setRoleAdminCount();
    }

    /***
     * 给用户分配角色
     * @author Cr Mr
     * @date 2022/4/3 14:20
     */
    @Override
    public void insertReoles(Long adminId, Long[] roleIds) {
        umsAdminMapper.deleteReoles(adminId);
        umsAdminMapper.insertReoles(adminId, roleIds);
        asyncTimeApi.setRoleAdminCount();
    }

    @Override
    public void update(UmsAdmin admin) {
        umsAdminMapper.update(admin, new LambdaQueryWrapper<UmsAdmin>().eq(UmsAdmin::getId, admin.getId()));
    }

    @Override
    public void updatePassword(String username, String password) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("username", username);
        updateWrapper.set("password", password);
        umsAdminMapper.update(null, updateWrapper);
    }

    @Override
    public UmsAdmin selectById(Long userId) {
        return umsAdminMapper.selectById(userId);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", id);
        updateWrapper.set(status != null, "status", status);
        umsAdminMapper.update(null, updateWrapper);
    }

    @Override
    public void saveLoginTime(Long id) {
        UpdateWrapper<UmsAdmin> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        wrapper.set("login_time", DateTime.now().toString("yyyy-MM-dd HH:mm:ss"));
        umsAdminMapper.update(null, wrapper);
    }

    @Override
    public void insert(UmsAdmin umsAdmin) {
        umsAdminMapper.insert(umsAdmin);
    }

}
